#include "devicepanel.h"
#include "cryptutils.h"

#include <QLabel>
#include <QUrl>
#include <QDebug>



// 初始化静态成员
const char DevicePanel::moveCommandValues[14] = {'u', 'd', 'l', 'r', '1', '2', '3', '4', 'n', 'f', 'i', 'o', 's'};



DevicePanel::DevicePanel(QWidget *parent) : QWidget(parent)
{
    // 创建主布局
    mainLayout = new QVBoxLayout(this);

    // 创建按钮组
    cameraControlButtonGroup = new QButtonGroup(this);

    createCameraControlUI();
}


void DevicePanel::createCameraControlUI()
{

    QGridLayout *controlLayout = new QGridLayout();

    // 创建方向控制按钮
    btnUp = new QPushButton("↑", this);
    btnDown = new QPushButton("↓", this);
    btnLeft = new QPushButton("←", this);
    btnRight = new QPushButton("→", this);
    btnLeftUp = new QPushButton("↖", this);
    btnLeftDown = new QPushButton("↙", this);
    btnRightUp = new QPushButton("↗", this);
    btnRightDown = new QPushButton("↘", this);

    // 创建变倍控制按钮
    btnZoomIn = new QPushButton("+", this);
    btnZoomOut = new QPushButton("-", this);

    // 创建聚焦控制按钮
    btnFocusIn = new QPushButton("+", this);
    btnFocusOut = new QPushButton("-", this);

    // 创建停止按钮
    QPushButton *stopButton = new QPushButton("停止");

    // 将按钮添加到按钮组并设置ID
    cameraControlButtonGroup->addButton(btnUp, CAMERA_UP);
    cameraControlButtonGroup->addButton(btnDown, CAMERA_DOWN);
    cameraControlButtonGroup->addButton(btnLeft, CAMERA_LEFT);
    cameraControlButtonGroup->addButton(btnRight, CAMERA_RIGHT);
    cameraControlButtonGroup->addButton(btnLeftUp, CAMERA_LEFT_UP);
    cameraControlButtonGroup->addButton(btnRightUp, CAMERA_RIGHT_UP);
    cameraControlButtonGroup->addButton(btnLeftDown, CAMERA_LEFT_DOWN);
    cameraControlButtonGroup->addButton(btnRightDown, CAMERA_RIGHT_DOWN);
    cameraControlButtonGroup->addButton(btnZoomIn, CAMERA_ZOOM_IN);
    cameraControlButtonGroup->addButton(btnZoomOut, CAMERA_ZOOM_OUT);
    cameraControlButtonGroup->addButton(btnFocusIn, CAMERA_FOCUS_IN);
    cameraControlButtonGroup->addButton(btnFocusOut, CAMERA_FOCUS_OUT);
    cameraControlButtonGroup->addButton(stopButton, CAMERA_STOP);

    // 为按钮连接按下和释放信号
    for (QAbstractButton *button : cameraControlButtonGroup->buttons()) {
        connect(button, &QPushButton::pressed, this, &DevicePanel::handleButtonPressed);
        connect(button, &QPushButton::released, this, &DevicePanel::handleButtonReleased);
    }

    // 布局按钮
    // 方向控制按钮布局
    controlLayout->addWidget(btnUp, 0, 1);
    controlLayout->addWidget(btnLeft, 1, 0);
    controlLayout->addWidget(stopButton, 1, 1);
    controlLayout->addWidget(btnRight, 1, 2);
    controlLayout->addWidget(btnDown, 2, 1);

    controlLayout->addWidget(btnLeftUp, 0, 0);
    controlLayout->addWidget(btnRightUp, 0, 2);
    controlLayout->addWidget(btnLeftDown, 2, 0);
    controlLayout->addWidget(btnRightDown, 2, 2);

    // 变焦和聚焦控制按钮布局
    controlLayout->addWidget(btnZoomIn, 3, 0);
    controlLayout->addWidget(btnZoomOut, 3, 1);
    controlLayout->addWidget(btnFocusIn, 4, 0);
    controlLayout->addWidget(btnFocusOut, 4, 1);

    mainLayout->addLayout(controlLayout);
}

DevicePanel::~DevicePanel()
{
}

QString DevicePanel::getMoveUrl(MoveCommand command)
{
    QString temp(cameraControlUrl);
    temp.append("/xsw/api/ptz/control");

    char cmdChar = moveCommandValues[static_cast<int>(command)];

    KVQuery kvs;
    kvs.add("channeId",QString::number(cameraChannelId));
    kvs.add("speed",QString::number(cameraSpeed));
    kvs.add("value",QString(cmdChar));

    QUrl res(temp);
    res.setQuery(kvs.getQueryParameter());

    return res.toString();
}


void DevicePanel::handleButtonPressed()
{
    // 获取被按下的按钮
    QAbstractButton *button = qobject_cast<QAbstractButton*>(sender());
    if (!button) return;

    // 获取按钮ID
    int id = cameraControlButtonGroup->id(button);
    MoveCommand command = static_cast<MoveCommand>(id);

    // 获取控制URL
    QString url = getMoveUrl(command);

    // 发送控制命令
    qDebug() << "Button pressed, sending command:" << url;

    emit ptzCommand(url);

    // 这里需要添加实际的HTTP请求代码
}

void DevicePanel::handleButtonReleased()
{
    // 按钮释放时发送停止命令
    QString url = getMoveUrl(CAMERA_STOP);

    // 发送停止命令
    qDebug() << "Button released, sending stop command:" << url;
    // 这里需要添加实际的HTTP请求代码

    emit ptzCommand(url);

    emit recordinfo(QTime::currentTime().toString() , QString("摄像头移动"));
}



void DevicePanel::setCameraControlUrl(const QString &url)
{
    cameraControlUrl = url;
}

void DevicePanel::setCameraSpeed(int speed)
{
    cameraSpeed = speed;
}

void DevicePanel::setCameraChannelId(uint32_t channelId)
{
    cameraChannelId = channelId;
}


